package view.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JTextArea;

public class NodeView extends JComponent implements MouseMotionListener,
		MouseListener {

	private NodesRenderer parentRenderer;

	private static final long serialVersionUID = 5555935548759342632L;

	private String name;

	private final static Color SELECTED_BACKGROUND = new Color(0xffff88);
	private final static Color UNSELECTED_BACKGROUND = new Color(0xeeee88);

	private boolean dragged = false;
	private int offsetX = 0;
	private int offsetY = 0;

	private JTextArea test;

	private void createUI() {
		this.setLayout(new BorderLayout());
		test = new JTextArea(
				"Ipsus lorem dolor simplet est Ipsus lorem dolor simplet est Ipsus lorem dolor simplet est Ipsus lorem dolor simplet est Ipsus lorem dolor simplet est Ipsus lorem dolor simplet est ");
		test.setLineWrap(true);
		// JLabel header = new JLabel("Head");
		JLabel bottom = new JLabel("Bottom");
		this.add(test, BorderLayout.CENTER);
		// this.add(header, BorderLayout.NORTH);
		this.add(bottom, BorderLayout.SOUTH);
	}

	public NodeView(NodesRenderer parentRenderer) {
		this.parentRenderer = parentRenderer;
		createUI();
		this.addMouseMotionListener(this);
		this.addMouseListener(this);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getCenterX() {
		return this.getX() + (this.getWidth() / 2);
	}

	public int getCenterY() {
		return this.getY() + (this.getHeight() / 2);
	}

	@Override
	protected void paintComponent(Graphics myGraphics) {
		Graphics2D graphics = (Graphics2D) myGraphics;
		super.paintComponent(graphics);
		if (this.dragged) {
			graphics.setColor(SELECTED_BACKGROUND);
		} else {
			graphics.setColor(UNSELECTED_BACKGROUND);
		}
		graphics.fillRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
		graphics.setColor(Color.YELLOW);
		graphics.fillRect(0, 0, this.getWidth() - 1, 15);
		graphics.setColor(Color.BLACK);
		graphics.drawLine(0, 15, this.getWidth() - 1, 15);
		graphics.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
		graphics.drawChars(name.toCharArray(), 0, name.length(), 2, 13);
		// graphics.dispose();
	}

	@Override
	public void mouseDragged(MouseEvent event) {

		int currentX = this.getLocation().x;
		int currentY = this.getLocation().y;
		int xPosition = event.getX();
		int yPosition = event.getY();

		boolean heightResizeOk = (yPosition > this.getHeight() - 5)
				&& this.getHeight() >= 5;
		boolean widthResizeOk = (xPosition > this.getWidth() - 5)
				&& this.getWidth() >= 5;

		if (heightResizeOk && widthResizeOk) {
			this.setBounds(this.getX(), this.getY(), xPosition, yPosition);
		} else if (widthResizeOk) {
			this.setBounds(this.getX(), this.getY(), xPosition,
					this.getHeight());
		} else if (heightResizeOk) {
			this.setBounds(this.getX(), this.getY(), this.getWidth(), yPosition);
		} else {
			this.setLocation(currentX + xPosition - offsetX, currentY
					+ yPosition - offsetY);
		}

		this.repaint();

		this.getParent().repaint();

	}

	@Override
	public void mouseMoved(MouseEvent event) {
	}

	@Override
	public void mouseClicked(MouseEvent event) {
		this.getParent().setComponentZOrder(this, 0);
		this.validate();
	}

	@Override
	public void mouseEntered(MouseEvent event) {
		this.dragged = true;
	}

	@Override
	public void mouseExited(MouseEvent event) {
		this.dragged = false;
	}

	@Override
	public void mousePressed(MouseEvent event) {
		this.getParent().setComponentZOrder(this, 0);
		this.validate();
		this.offsetX = event.getX();
		this.offsetY = event.getY();
	}

	@Override
	public void mouseReleased(MouseEvent event) {
	}

}
